﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Lottery
{
    public partial class Form1 : Form
    {
        private int TempRandom()
        {
            return 0;
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            Program.state = Program.AppState.THIRD;
            Program.substate = Program.SubState.ONE;

            FileInfo fi = new FileInfo("people.txt");
            if (!fi.Exists)
            {
                MessageBox.Show("名单文件不存在！");
                this.Close();
            }
            Program.people = new Dictionary<string, List<string>>();
            Program.people2 = new Dictionary<string, List<string>>();
            Program.major = new List<string>();
            Program.temp = new List<string>();
            FileStream peopleFile = fi.OpenRead();
            long length = peopleFile.Length;
            byte[] byteBuffer = new byte[length];
            int lengthRead = peopleFile.Read(byteBuffer, 0, (int)length);
            string readed = System.Text.Encoding.Default.GetString(byteBuffer);
            int start = 0;
            int finish = 0;
            for (int count = 0; count <= readed.Length; ++ count)
            {
                if ((count == readed.Length) || (readed[count] == 0x0D))
                {
                    finish = count - 1;
                    for (int count2 = start; count2 < finish; ++ count2)
                    {
                        if (readed[count2] == '_')
                        {
                            string major = readed.Substring(start, count2 - start);
                            string name = readed.Substring(count2 + 1, finish - count2);
                            if (Program.people.ContainsKey(major))
                            {
                                Program.people[major].Add(name);
                            }
                            else
                            {
                                List<string> names = new List<string>();
                                names.Add(name);
                                Program.major.Add(major);
                                Program.people.Add(major, names);
                            }
                            List<string> temp = new List<string>();
                            temp.Add(major);
                            temp.Add(name);
                            Program.people2.Add(major + name, temp);
                            Program.temp.Add(major + name);
//                             if (Program.temp.Count < 150)
//                             {
//                                 MessageBox.Show("人数不足");
//                                 Application.Exit();
//                             }
                        }
                    }
                    start = count + 2;
                }
            }
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Return)
            {
                if (Program.state == Program.AppState.OVER)
                {
                    this.Close();
                }
                else
                {
                    int state = 1;
                    switch (Program.state)
                    {
                        case Program.AppState.THIRD:
                            state = 1;
                            break;
                        case Program.AppState.SECOND:
                            state = 2;
                            break;
                        case Program.AppState.FIRST:
                            state = 3;
                            break;
                        case Program.AppState.BEST:
                            state = 4;
                            break;
                        default:
                            break;
                    }
                    LotteryForm form = new LotteryForm(state);
                    form.ShowDialog();
                }
            }
        }
    }
}
